from PyQt4.QtCore import QObject

from random import randint

from config import *
from autoplayer.evaluate import Evaluator
from autoplayer.checkmate import Checkmater
from autoplayer.minmax import Searcher
from autoplayer.zobrist import Zobrist


class AutoPlayer(QObject):
    def __init__(self):
        self.role = None
        self.model = None
        self.zobrist = Zobrist()
        self.evaluator = Evaluator()
        self.checkmater = Checkmater()
        self.searcher = Searcher()

    def setup(self, model=None, role=ROLES.vacant):
        self.role = role
        self.model = model
        self.zobrist.setup(Len, self.role)
        self.evaluator.setup(role, model, self.zobrist)
        self.checkmater.setup(role, model, self.zobrist, self.evaluator)
        self.searcher.setup(role, model, self.zobrist, self.evaluator, self.checkmater)

    def react(self):
        return self.normal_react()

    def normal_react(self):
        self.evaluator.update_point()
        result = self.searcher.search()
        return result[0], result[1]

    def rand_react(self):
        while True:
            x = randint(0, 14)
            y = randint(0, 14)
            if self.model.get_pos(x, y) == ROLES.vacant:
                return x, y
